package view;

import controller.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import model.information.Memo;

/**
 *
 * @author tobias
 */
public class MemoPanel extends javax.swing.JPanel {
    private Controller ctrl;
    private ArrayList<Memo> memos;
    private Memo currentMemo;

    /** Creates new form MemoPanel */
    public MemoPanel(Controller ctrl) {
        initComponents();
        this.ctrl = ctrl;
        memos = ctrl.getMemos();
        updateList();
    }

    public void updateList() {
        memoList.setListData(memos.toArray());
    }

    public void updateFromSave(Object o) {
        memoList.repaint();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        listPanel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        memoList = new javax.swing.JList();
        addBtn = new javax.swing.JButton();
        remBtn = new javax.swing.JButton();
        textPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();
        saveBtn = new javax.swing.JButton();

        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(816, 586));

        jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() | java.awt.Font.BOLD, jLabel1.getFont().getSize()+3));
        jLabel1.setText("Huskesedler");

        listPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Huskesedler:"));
        listPanel.setOpaque(false);
        listPanel.setPreferredSize(new java.awt.Dimension(230, 518));

        memoList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        memoList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                memoListValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(memoList);

        addBtn.setText("Opret ny note");
        addBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addBtnActionPerformed(evt);
            }
        });

        remBtn.setText("Slet note");
        remBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                remBtnActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout listPanelLayout = new org.jdesktop.layout.GroupLayout(listPanel);
        listPanel.setLayout(listPanelLayout);
        listPanelLayout.setHorizontalGroup(
            listPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, listPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(listPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, remBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, addBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE))
                .addContainerGap())
        );
        listPanelLayout.setVerticalGroup(
            listPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, listPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addBtn)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(remBtn)
                .addContainerGap())
        );

        textPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Tekst:"));
        textPanel.setOpaque(false);
        textPanel.setPreferredSize(new java.awt.Dimension(532, 518));

        textArea.setColumns(20);
        textArea.setLineWrap(true);
        textArea.setRows(5);
        textArea.setWrapStyleWord(true);
        textArea.setEnabled(false);
        jScrollPane1.setViewportView(textArea);

        saveBtn.setText("Gem ændringer");
        saveBtn.setEnabled(false);
        saveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBtnActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout textPanelLayout = new org.jdesktop.layout.GroupLayout(textPanel);
        textPanel.setLayout(textPanelLayout);
        textPanelLayout.setHorizontalGroup(
            textPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, textPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(textPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
                    .add(saveBtn))
                .addContainerGap())
        );
        textPanelLayout.setVerticalGroup(
            textPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, textPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(saveBtn)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(listPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(textPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(jLabel1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(textPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(listPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void remBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remBtnActionPerformed
        if(memoList.getSelectedIndex() == -1) {
            JOptionPane.showMessageDialog(ctrl.getFrame(), "Der er ikke valgt en huskeseddel til sletning, vælg venligst en og prøv igen.", "Vælg huskeseddel", JOptionPane.WARNING_MESSAGE);
        } else {
            ctrl.removeMemo(currentMemo);
        }
}//GEN-LAST:event_remBtnActionPerformed

    private void memoListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_memoListValueChanged
        if(memoList.getSelectedIndex() != -1) {
            currentMemo = (Memo)memoList.getSelectedValue();
            textArea.setText(currentMemo.getNote());
            textArea.setEnabled(true);
            saveBtn.setEnabled(true);
        } else {
            textArea.setText("");
            textArea.setEnabled(false);
            saveBtn.setEnabled(false);
        }
    }//GEN-LAST:event_memoListValueChanged

    private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBtnActionPerformed
        currentMemo.setNote(textArea.getText());
    }//GEN-LAST:event_saveBtnActionPerformed

    private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addBtnActionPerformed
        Memo newMemo = ctrl.addMemo("");
        memoList.setSelectedValue(newMemo, true);
        textArea.requestFocus();
    }//GEN-LAST:event_addBtnActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel listPanel;
    private javax.swing.JList memoList;
    private javax.swing.JButton remBtn;
    private javax.swing.JButton saveBtn;
    private javax.swing.JTextArea textArea;
    private javax.swing.JPanel textPanel;
    // End of variables declaration//GEN-END:variables

}